package proxy

import (
	"net/http"
	"sync"
)

var (
	serverData = make(map[string]*http.Server)
	serverLock sync.RWMutex

	serverStatus     = make(map[string]bool)
	serverStatusLock sync.RWMutex
)

type RestartData struct {
	ServerName string `json:"serverName"`
}

func setServer(name string, server *http.Server) {
	serverLock.Lock()
	defer serverLock.Unlock()
	serverData[name] = server
}

func getServer(name string) *http.Server {
	serverLock.RLock()
	defer serverLock.RUnlock()
	return serverData[name]
}

func setServerStatus(name string, status bool) {
	serverStatusLock.Lock()
	defer serverStatusLock.Unlock()
	serverStatus[name] = status
}

func GetServerStatus(name string) bool {
	serverStatusLock.RLock()
	defer serverStatusLock.RUnlock()
	return serverStatus[name]
}

func GetRunningServerNum() int {
	serverStatusLock.RLock()
	defer serverStatusLock.RUnlock()

	num := 0
	for _, status := range serverStatus {
		if status {
			num += 1
		}
	}
	return num
}
